package com.hkfn.rfb.rfb.fragment;

import android.app.Dialog;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.hkfn.rfb.MainActivity;
import com.hkfn.rfb.common.activity.BaseActivity;
import com.hkfn.rfb.common.bean.BannerModel;
import com.hkfn.rfb.common.bean.ConstData;
import com.hkfn.rfb.common.fragment.BaseFragment;
import com.hkfn.rfb.common.utils.DialogUtils;
import com.hkfn.rfb.common.utils.StringUtils;
import com.hkfn.rfb.common.utils.ToastUtils;
import com.hkfn.rfb.databinding.FRfbProjectManageBinding;
import com.hkfn.rfb.rfb.adapter.RfbProjectManageAdapter;
import com.hkfn.rfb.rfb.bean.RfbDataModel;
import com.hkfn.rfb.rfb.bean.RfbParamManager;
import com.hkfn.rfb.rfb.bean.RfbParamModel;
import com.hkfn.rfb.rfb.bean.RfbProjectManageModel;
import com.xh.common.thread.AsyncExecutor;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 项目管理
 */
public class RfbProjectManageFragment extends BaseFragment implements RfbProjectManageAdapter.ProjectManageListCallBack {


    private BaseActivity activity;
    private Bundle bundle;
    private FRfbProjectManageBinding fRfbProjectManageBinding;

    private List<RfbProjectManageModel> list;
    private RfbProjectManageAdapter rfbProjectManageAdapter;

    private Dialog datePickerDialog;
    private String currentDateStr;

    private Dialog chooseProjectTypeDialog;
    private Dialog chooseProjectStatusDialog;

    private Dialog deleteSureDialog;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        activity = getMyActivity();
        bundle = getArguments();
        fRfbProjectManageBinding = FRfbProjectManageBinding.inflate(inflater, container, false);
        initListData();
        initView();
        setListData();
        return fRfbProjectManageBinding.getRoot();
    }

    private void initListData() {
        list = new ArrayList<>();
        for (int i = 0; i < 50; i++) {
            RfbProjectManageModel model = new RfbProjectManageModel();
            model.set_id(i);
            model.generateData();
            list.add(model);
        }


    }

    private void initView() {
        initHeadSelectView();

        initProjectListView();
    }

    private void initHeadSelectView() {
        fRfbProjectManageBinding.tvSelectDate.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String dateStr = fRfbProjectManageBinding.tvSelectDate.getText().toString();
                if (!dateStr.equals(currentDateStr)){
                    currentDateStr = dateStr;
                    setListData();
                }

            }
        });
        fRfbProjectManageBinding.llSelectDate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (fRfbProjectManageBinding.llDetailContent.getVisibility() == View.VISIBLE || fRfbProjectManageBinding.llAddContent.getVisibility() == View.VISIBLE){
                    return;
                }
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(new Date());
                calendar.add(Calendar.MONTH, -1);
                DialogUtils.closeDialog(datePickerDialog);
                datePickerDialog = DialogUtils.createDatePickerDialog(activity, fRfbProjectManageBinding.tvSelectDate,
                        calendar.getTimeInMillis(), System.currentTimeMillis());
                datePickerDialog.show();
            }
        });

        fRfbProjectManageBinding.tvProjectType.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (fRfbProjectManageBinding.llDetailContent.getVisibility() == View.VISIBLE || fRfbProjectManageBinding.llAddContent.getVisibility() == View.VISIBLE){
                    return;
                }
                DialogUtils.closeDialog(chooseProjectTypeDialog);
                List<BannerModel> bannerModels = new ArrayList<>();
                BannerModel bannerModel = new BannerModel();
                bannerModel.setTitle("项目类型1");
                bannerModels.add(bannerModel);
                BannerModel bannerModel1 = new BannerModel();
                bannerModel1.setTitle("项目类型2");
                bannerModels.add(bannerModel1);
                BannerModel bannerModel2 = new BannerModel();
                bannerModel2.setTitle("项目类型3");
                bannerModels.add(bannerModel2);
                chooseProjectTypeDialog = DialogUtils.createChooseListDialog(activity, bannerModels, "选择项目类型", -1, (v12, position, data) -> {
                    fRfbProjectManageBinding.tvProjectType.setText(((BannerModel)data).getTitle());
                    setListData();
                    DialogUtils.closeDialog(chooseProjectTypeDialog);
                });
                DialogUtils.showDialog(chooseProjectTypeDialog);
            }
        });

        fRfbProjectManageBinding.tvProjectStatus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (fRfbProjectManageBinding.llDetailContent.getVisibility() == View.VISIBLE || fRfbProjectManageBinding.llAddContent.getVisibility() == View.VISIBLE){
                    return;
                }
                DialogUtils.closeDialog(chooseProjectStatusDialog);
                List<BannerModel> bannerModels = new ArrayList<>();
                BannerModel bannerModel = new BannerModel();
                bannerModel.setTitle("未开始");
                bannerModels.add(bannerModel);
                BannerModel bannerModel2 = new BannerModel();
                bannerModel2.setTitle("进行中");
                bannerModels.add(bannerModel2);
                BannerModel bannerModel1 = new BannerModel();
                bannerModel1.setTitle("已完成");
                bannerModels.add(bannerModel1);
                chooseProjectStatusDialog = DialogUtils.createChooseListDialog(activity, bannerModels, "选择项目状态", -1, (v1, position, data) -> {
                    fRfbProjectManageBinding.tvProjectStatus.setText(((BannerModel)data).getTitle());
                    setListData();
                    DialogUtils.closeDialog(chooseProjectStatusDialog);
                });
                DialogUtils.showDialog(chooseProjectStatusDialog);
            }
        });

        fRfbProjectManageBinding.tvClearSelect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                currentDateStr = "";
                fRfbProjectManageBinding.tvSelectDate.setText("选择时间");
                fRfbProjectManageBinding.tvProjectType.setText("项目类型");
                fRfbProjectManageBinding.tvProjectStatus.setText("项目状态");
            }
        });

        fRfbProjectManageBinding.tvAddProject.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addAction();
            }
        });
    }

    private void initProjectListView(){
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(activity);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        fRfbProjectManageBinding.rlvRfbFormData.setLayoutManager(linearLayoutManager);
        rfbProjectManageAdapter = new RfbProjectManageAdapter(activity, this);
        fRfbProjectManageBinding.rlvRfbFormData.setAdapter(rfbProjectManageAdapter);
    }

    private void setHeadNum(){
        if (null != list && list.size() > 0){
            int completeNum = 0;
            int doingNum = 0;
            for (RfbProjectManageModel rfbProjectManageModel : list) {
                if ("2".equals(rfbProjectManageModel.getProjectStatus())){
                    // 已完成
                    completeNum++;
                }else if ("1".equals(rfbProjectManageModel.getProjectStatus())){
                    // 进行中
                    doingNum++;
                }
            }
            fRfbProjectManageBinding.tvProjectAllNum.setText("" + list.size());
            fRfbProjectManageBinding.tvProjectCompleteNum.setText("" + completeNum);
            fRfbProjectManageBinding.tvProjectDoingNum.setText("" + doingNum);
        }else {
            fRfbProjectManageBinding.tvProjectCompleteNum.setText("0");
            fRfbProjectManageBinding.tvProjectAllNum.setText("0");
            fRfbProjectManageBinding.tvProjectDoingNum.setText("0");
        }
    }

    private void setListData(){
        rfbProjectManageAdapter.clear();
        AsyncExecutor.execute(new Runnable() {
            @Override
            public void run() {
                long startTime = 0;
                if (!StringUtils.isEmpty(currentDateStr)) {
                    Date chooseDate = null;
                    try {
                        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(ConstData.TIME_FORMAT_YYYY_MM_DD);
                        chooseDate = simpleDateFormat.parse(currentDateStr);
                    } catch (ParseException e) {

                    }
                    if (null != chooseDate) {
                        Calendar calendar = Calendar.getInstance();
                        calendar.setTime(chooseDate);
                        calendar.set(Calendar.HOUR_OF_DAY, 0);
                        calendar.set(Calendar.MINUTE, 0);
                        calendar.set(Calendar.SECOND, 0);
                        calendar.set(Calendar.MILLISECOND, 0);
                        startTime = calendar.getTimeInMillis();
                    }
                }

                String projectType = null;
                switch (fRfbProjectManageBinding.tvProjectType.getText().toString()){
                    case "项目类型1":
                        projectType = "0";
                        break;
                    case "项目类型2":
                        projectType = "1";
                        break;
                    case "项目类型3":
                        projectType = "2";
                        break;
                }
                String projectStatus = null;
                switch (fRfbProjectManageBinding.tvProjectStatus.getText().toString()){
                    case "未完成":
                        projectStatus = "0";
                        break;
                    case "进行中":
                        projectStatus = "1";
                        break;
                    case "已完成":
                        projectStatus = "2";
                        break;
                }
                List<RfbProjectManageModel> rfbProjectManageModels = new ArrayList<>();
                for (int i = 0; i < list.size(); i++) {
                    RfbProjectManageModel model = list.get(i);
                    if (!StringUtils.isEmpty(projectType) && !projectType.equals(model.getProjectType())){
                        continue;
                    }
                    if (!StringUtils.isEmpty(projectStatus) && !projectStatus.equals(model.getProjectStatus())){
                        continue;
                    }
                    if (0 != startTime && startTime > model.getStartTime()){
                        continue;
                    }
                    rfbProjectManageModels.add(model);
                }
                activity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        rfbProjectManageAdapter.addDatas(rfbProjectManageModels);
                        setHeadNum();
                    }
                });

            }
        });
    }

    @Override

    public void onResume() {
        super.onResume();
        updateViewData();
        ((MainActivity) activity).setMenuTitle(ConstData.EMS_MAIN);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        fRfbProjectManageBinding = null;
        DialogUtils.closeDialog(datePickerDialog);
        DialogUtils.closeDialog(chooseProjectTypeDialog);
        DialogUtils.closeDialog(chooseProjectStatusDialog);
        DialogUtils.closeDialog(deleteSureDialog);
    }

    @Override
    public void updateViewData() {
        super.updateViewData();

    }

    private void addAction(){
        if (fRfbProjectManageBinding.llDetailContent.getVisibility() == View.VISIBLE || fRfbProjectManageBinding.llAddContent.getVisibility() == View.VISIBLE){
            return;
        }
        fRfbProjectManageBinding.llAddContent.setVisibility(View.VISIBLE);
        fRfbProjectManageBinding.etAddProjectName.setText("");
        fRfbProjectManageBinding.tvAddProjectType.setText("选择项目类型");
        fRfbProjectManageBinding.tvAddTestObject.setText("选择测试对象");
        fRfbProjectManageBinding.tvAddStartTime.setText("开始时间");
        fRfbProjectManageBinding.tvAddEndTime.setText("结束时间");
        fRfbProjectManageBinding.etAddClientName.setText("");
        fRfbProjectManageBinding.etAddPersonName.setText("");
        fRfbProjectManageBinding.etAddProjectDescription.setText("");
        fRfbProjectManageBinding.tvAddTestDevice.setText("选择设备");

        fRfbProjectManageBinding.tvAddStartTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(new Date());
                calendar.add(Calendar.MONTH, 1);
                DialogUtils.closeDialog(datePickerDialog);
                datePickerDialog = DialogUtils.createDatePickerDialog(activity, fRfbProjectManageBinding.tvAddStartTime,
                        System.currentTimeMillis(),  calendar.getTimeInMillis());
                datePickerDialog.show();
            }
        });
        fRfbProjectManageBinding.tvAddEndTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(new Date());
                calendar.add(Calendar.MONTH, 1);
                DialogUtils.closeDialog(datePickerDialog);
                datePickerDialog = DialogUtils.createDatePickerDialog(activity, fRfbProjectManageBinding.tvAddEndTime,
                        System.currentTimeMillis(),  calendar.getTimeInMillis());
                datePickerDialog.show();
            }
        });
        fRfbProjectManageBinding.llAddProjectType.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogUtils.closeDialog(chooseProjectTypeDialog);
                List<BannerModel> bannerModels = new ArrayList<>();
                BannerModel bannerModel = new BannerModel();
                bannerModel.setTitle("项目类型1");
                bannerModels.add(bannerModel);
                BannerModel bannerModel1 = new BannerModel();
                bannerModel1.setTitle("项目类型2");
                bannerModels.add(bannerModel1);
                BannerModel bannerModel2 = new BannerModel();
                bannerModel2.setTitle("项目类型3");
                bannerModels.add(bannerModel2);
                chooseProjectTypeDialog = DialogUtils.createChooseListDialog(activity, bannerModels, "选择项目类型", -1, (v12, position, data) -> {
                    fRfbProjectManageBinding.tvAddProjectType.setText(((BannerModel)data).getTitle());
                    DialogUtils.closeDialog(chooseProjectTypeDialog);
                });
                DialogUtils.showDialog(chooseProjectTypeDialog);
            }
        });
        fRfbProjectManageBinding.llAddTestDevice.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogUtils.closeDialog(chooseProjectTypeDialog);
                List<BannerModel> bannerModels = new ArrayList<>();
                BannerModel bannerModel = new BannerModel();
                bannerModel.setTitle("设备1");
                bannerModels.add(bannerModel);
                BannerModel bannerModel1 = new BannerModel();
                bannerModel1.setTitle("设备2");
                bannerModels.add(bannerModel1);
                BannerModel bannerModel2 = new BannerModel();
                bannerModel2.setTitle("设备3");
                bannerModels.add(bannerModel2);
                chooseProjectTypeDialog = DialogUtils.createChooseListDialog(activity, bannerModels, "选择设备", -1, (v12, position, data) -> {
                    fRfbProjectManageBinding.tvAddTestDevice.setText(((BannerModel)data).getTitle());
                    DialogUtils.closeDialog(chooseProjectTypeDialog);
                });
                DialogUtils.showDialog(chooseProjectTypeDialog);
            }
        });
        fRfbProjectManageBinding.llAddTestObject.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogUtils.closeDialog(chooseProjectTypeDialog);
                List<BannerModel> bannerModels = new ArrayList<>();
                for (RfbParamModel rfbParamModel : RfbParamManager.batteryParamManager) {
                    BannerModel bannerModel = new BannerModel();
                    bannerModel.setTitle(rfbParamModel.getParamName());
                    bannerModels.add(bannerModel);
                }
                chooseProjectTypeDialog = DialogUtils.createChooseListDialog(activity, bannerModels, "选择测试对象", -1, (v12, position, data) -> {
                    fRfbProjectManageBinding.tvAddTestObject.setText(((BannerModel)data).getTitle());
                    DialogUtils.closeDialog(chooseProjectTypeDialog);
                });
                DialogUtils.showDialog(chooseProjectTypeDialog);
            }
        });

        fRfbProjectManageBinding.tvAddCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                fRfbProjectManageBinding.llAddContent.setVisibility(View.GONE);
            }
        });
        fRfbProjectManageBinding.tvAddSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RfbProjectManageModel model = getAddOrModifyModel();
                if (null != model){
                    model.setProjectStatus("0");
                    list.add(0, model);
                    setListData();
                    fRfbProjectManageBinding.llAddContent.setVisibility(View.GONE);
                }else {
                    ToastUtils.showToast(activity, "数据错误");
                }
            }
        });
    }

    @Override
    public void operaAction(int position) {
        if (fRfbProjectManageBinding.llDetailContent.getVisibility() == View.VISIBLE || fRfbProjectManageBinding.llAddContent.getVisibility() == View.VISIBLE){
            return;
        }
        RfbProjectManageModel rfbProjectManageModel = list.get(position);
        fRfbProjectManageBinding.llDetailContent.setVisibility(View.VISIBLE);
        fRfbProjectManageBinding.tvDetailProjectName.setText(rfbProjectManageModel.getProjectName());
        fRfbProjectManageBinding.tvDetailProjectType.setText(rfbProjectManageModel.obtainTypeStr());
        fRfbProjectManageBinding.tvDetailTestObject.setText(rfbProjectManageModel.obtainTestObjectStr());
        fRfbProjectManageBinding.tvDetailStartTime.setText(rfbProjectManageModel.obtainStartTimeStr());
        fRfbProjectManageBinding.tvDetailEndTime.setText(rfbProjectManageModel.obtainStopTimeStr());
        fRfbProjectManageBinding.tvDetailClientName.setText(rfbProjectManageModel.getClientName());
        fRfbProjectManageBinding.tvDetailPersonName.setText(rfbProjectManageModel.getStartPerson());
        fRfbProjectManageBinding.tvDetailProjectDescription.setText(rfbProjectManageModel.getProjectDescription());
        fRfbProjectManageBinding.tvDetailTestContent.setText(rfbProjectManageModel.getTestContent());
        fRfbProjectManageBinding.tvDetailDeviceName.setText(rfbProjectManageModel.getDeviceInfo());

        fRfbProjectManageBinding.tvDetailCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                fRfbProjectManageBinding.llDetailContent.setVisibility(View.GONE);
            }
        });
        fRfbProjectManageBinding.tvDetailSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                fRfbProjectManageBinding.llDetailContent.setVisibility(View.GONE);
            }
        });
    }

    private RfbProjectManageModel getAddOrModifyModel(){
        RfbProjectManageModel model = new RfbProjectManageModel();
        String projectName = fRfbProjectManageBinding.etAddProjectName.getText().toString();
        if (StringUtils.isEmpty(projectName)){
            return null;
        }
        model.setProjectName(projectName);

        String projectType = fRfbProjectManageBinding.tvAddProjectType.getText().toString();
        if (StringUtils.isEmpty(projectType) || "选择项目类型".equals(projectType)){
            return null;
        }
        model.setTypeStr(projectType);

        String testObjectStr = fRfbProjectManageBinding.tvAddTestObject.getText().toString();
        if (StringUtils.isEmpty(testObjectStr) || "选择测试对象".equals(testObjectStr)){
            return null;
        }
        List<RfbDataModel> rfbDataModels = new ArrayList<>();
        String[] split = testObjectStr.split(";");
        for (String s : split) {
            RfbDataModel rfbDataModel = new RfbDataModel();
            rfbDataModel.setDataName(s);
            rfbDataModels.add(rfbDataModel);
        }
        model.setTestObject(rfbDataModels);

        String startTimeStr = fRfbProjectManageBinding.tvAddStartTime.getText().toString();
        String stopTimeStr = fRfbProjectManageBinding.tvAddEndTime.getText().toString();
        if (StringUtils.isEmpty(startTimeStr) || "开始时间".equals(startTimeStr) || StringUtils.isEmpty(stopTimeStr) || "结束时间".equals(stopTimeStr)){
            return null;
        }
        model.setStartTime(strToLong(startTimeStr));
        model.setStopTime(strToLong(stopTimeStr));

        String clientName = fRfbProjectManageBinding.etAddClientName.getText().toString();
        if (StringUtils.isEmpty(clientName)){
            return null;
        }
        model.setClientName(clientName);

        String personName = fRfbProjectManageBinding.etAddPersonName.getText().toString();
        if (StringUtils.isEmpty(personName)){
            return null;
        }
        model.setStartPerson(personName);

        String projectDescription = fRfbProjectManageBinding.etAddProjectDescription.getText().toString();
        if (StringUtils.isEmpty(projectDescription)){
            return null;
        }
        model.setProjectDescription(projectDescription);

        String deviceInfo = fRfbProjectManageBinding.tvAddTestDevice.getText().toString();
        if (StringUtils.isEmpty(deviceInfo) || "选择设备".equals(projectType)){
            return null;
        }
        model.setDeviceInfo(deviceInfo);

        return model;
    }

    private long strToLong(String dateStr){
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date date = dateFormat.parse(dateStr);
            return date.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public void modifyAction(int position) {
        if (fRfbProjectManageBinding.llDetailContent.getVisibility() == View.VISIBLE || fRfbProjectManageBinding.llAddContent.getVisibility() == View.VISIBLE){
            return;
        }
        RfbProjectManageModel rfbProjectManageModel = list.get(position);
        fRfbProjectManageBinding.llAddContent.setVisibility(View.VISIBLE);
        fRfbProjectManageBinding.etAddProjectName.setText(rfbProjectManageModel.getProjectName());
        fRfbProjectManageBinding.tvAddProjectType.setText(rfbProjectManageModel.obtainTypeStr());
        fRfbProjectManageBinding.tvAddTestObject.setText(rfbProjectManageModel.obtainTestObjectStr());
        fRfbProjectManageBinding.tvAddStartTime.setText(rfbProjectManageModel.obtainStartTimeStr());
        fRfbProjectManageBinding.tvAddEndTime.setText(rfbProjectManageModel.obtainStopTimeStr());
        fRfbProjectManageBinding.etAddClientName.setText(rfbProjectManageModel.getClientName());
        fRfbProjectManageBinding.etAddPersonName.setText(rfbProjectManageModel.getStartPerson());
        fRfbProjectManageBinding.etAddProjectDescription.setText(rfbProjectManageModel.getProjectDescription());
        fRfbProjectManageBinding.tvAddTestDevice.setText(rfbProjectManageModel.getDeviceInfo());
        fRfbProjectManageBinding.tvAddStartTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(new Date());
                calendar.add(Calendar.MONTH, 1);
                DialogUtils.closeDialog(datePickerDialog);
                datePickerDialog = DialogUtils.createDatePickerDialog(activity, fRfbProjectManageBinding.tvAddStartTime,
                        System.currentTimeMillis(),  calendar.getTimeInMillis());
                datePickerDialog.show();
            }
        });
        fRfbProjectManageBinding.tvAddEndTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(new Date());
                calendar.add(Calendar.MONTH, 1);
                DialogUtils.closeDialog(datePickerDialog);
                datePickerDialog = DialogUtils.createDatePickerDialog(activity, fRfbProjectManageBinding.tvAddEndTime,
                        System.currentTimeMillis(),  calendar.getTimeInMillis());
                datePickerDialog.show();
            }
        });
        fRfbProjectManageBinding.llAddProjectType.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogUtils.closeDialog(chooseProjectTypeDialog);
                List<BannerModel> bannerModels = new ArrayList<>();
                BannerModel bannerModel = new BannerModel();
                bannerModel.setTitle("项目类型1");
                bannerModels.add(bannerModel);
                BannerModel bannerModel1 = new BannerModel();
                bannerModel1.setTitle("项目类型2");
                bannerModels.add(bannerModel1);
                BannerModel bannerModel2 = new BannerModel();
                bannerModel2.setTitle("项目类型3");
                bannerModels.add(bannerModel2);
                chooseProjectTypeDialog = DialogUtils.createChooseListDialog(activity, bannerModels, "选择项目类型", -1, (v12, position, data) -> {
                    fRfbProjectManageBinding.tvAddProjectType.setText(((BannerModel)data).getTitle());
                    DialogUtils.closeDialog(chooseProjectTypeDialog);
                });
                DialogUtils.showDialog(chooseProjectTypeDialog);
            }
        });
        fRfbProjectManageBinding.llAddTestDevice.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogUtils.closeDialog(chooseProjectTypeDialog);
                List<BannerModel> bannerModels = new ArrayList<>();
                BannerModel bannerModel = new BannerModel();
                bannerModel.setTitle("设备1");
                bannerModels.add(bannerModel);
                BannerModel bannerModel1 = new BannerModel();
                bannerModel1.setTitle("设备2");
                bannerModels.add(bannerModel1);
                BannerModel bannerModel2 = new BannerModel();
                bannerModel2.setTitle("设备3");
                bannerModels.add(bannerModel2);
                chooseProjectTypeDialog = DialogUtils.createChooseListDialog(activity, bannerModels, "选择设备", -1, (v12, position, data) -> {
                    fRfbProjectManageBinding.tvAddTestDevice.setText(((BannerModel)data).getTitle());
                    DialogUtils.closeDialog(chooseProjectTypeDialog);
                });
                DialogUtils.showDialog(chooseProjectTypeDialog);
            }
        });
        fRfbProjectManageBinding.llAddTestObject.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogUtils.closeDialog(chooseProjectTypeDialog);
                List<BannerModel> bannerModels = new ArrayList<>();
                for (RfbParamModel rfbParamModel : RfbParamManager.batteryParamManager) {
                    BannerModel bannerModel = new BannerModel();
                    bannerModel.setTitle(rfbParamModel.getParamName());
                    bannerModels.add(bannerModel);
                }
                chooseProjectTypeDialog = DialogUtils.createChooseListDialog(activity, bannerModels, "选择测试对象", -1, (v12, position, data) -> {
                    fRfbProjectManageBinding.tvAddTestObject.setText(((BannerModel)data).getTitle());
                    DialogUtils.closeDialog(chooseProjectTypeDialog);
                });
                DialogUtils.showDialog(chooseProjectTypeDialog);
            }
        });
        fRfbProjectManageBinding.tvAddCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                fRfbProjectManageBinding.llAddContent.setVisibility(View.GONE);
            }
        });
        fRfbProjectManageBinding.tvAddSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RfbProjectManageModel model = getAddOrModifyModel();
                if (null != model){
                    list.remove(position);
                    list.add(position, model);
                    setListData();
                    fRfbProjectManageBinding.llAddContent.setVisibility(View.GONE);
                }else {
                    ToastUtils.showToast(activity, "数据错误");
                }
            }
        });
    }

    @Override
    public void deleteAction(int position) {
        if (fRfbProjectManageBinding.llDetailContent.getVisibility() == View.VISIBLE || fRfbProjectManageBinding.llAddContent.getVisibility() == View.VISIBLE){
            return;
        }
        deleteSureDialog = DialogUtils.createSureAndCancelDialog(activity, "是否删除该项目", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogUtils.closeDialog(deleteSureDialog);
            }
        }, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogUtils.closeDialog(deleteSureDialog);
                list.remove(position);
                setListData();
            }
        });
        DialogUtils.showDialog(deleteSureDialog);
    }
}
